﻿#include "circleshape.h"

CircleShape::CircleShape(QWidget* parent)
	: Shape(parent)
{
	shape_brush = QBrush(QColor(255, 255, 255));
}

void CircleShape::paintPixmap(QPainter& painter)
{
	painter.setPen(shape_pen);
	painter.setBrush(shape_brush);
	painter.drawEllipse(start_point.x(), start_point.y(), shape_width, shape_height);
}

void CircleShape::paintCircle(QPainter& painter)
{
	//默认蓝色外圈
	painter.setPen(QPen(QColor(6, 123, 239)));
	//默认白色内圈
	painter.setBrush(QBrush(QColor(255, 255, 255)));
	//中上
	painter.drawEllipse(QRect(start_point.x() + shape_width / 2 - circle_radius, start_point.y() - circle_radius, circle_radius * 2, circle_radius * 2));
	//左中
	painter.drawEllipse(QRect(start_point.x() - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2));
	//右中
	painter.drawEllipse(QRect(start_point.x() + shape_width - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2));
	//中下
	painter.drawEllipse(QRect(start_point.x() + shape_width / 2 - circle_radius, start_point.y() + shape_height - circle_radius, circle_radius * 2, circle_radius * 2));
}

bool CircleShape::nodesDetect(const QPoint& point)
{
	//包含四个结点，中上，左中，右中，中下，为了避免进行包含求根号的距离计算，我们将点击区域的圆看成一个正方形
	QRect rect_mt(start_point.x() + shape_width / 2 - circle_radius, start_point.y() - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_lm(start_point.x() - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_rm(start_point.x() + shape_width - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_mb(start_point.x() + shape_width / 2 - circle_radius, start_point.y() + shape_height - circle_radius, circle_radius * 2, circle_radius * 2);
	if (rect_mt.contains(point) || rect_lm.contains(point) || rect_rm.contains(point) || rect_mb.contains(point))
		return true;
	return false;
}

bool CircleShape::clickDetect(const QPoint& point)
{
	QRect rect(start_point.x(), start_point.y(), shape_width, shape_height);
	return rect.contains(point);
}

void CircleShape::setNodeIndex(const QPoint& point)
{
	//中上-1，左中-2，右中-3，中下-4
	QRect rect_mt(start_point.x() + shape_width / 2 - circle_radius, start_point.y() - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_lm(start_point.x() - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_rm(start_point.x() + shape_width - circle_radius, start_point.y() + shape_height / 2 - circle_radius, circle_radius * 2, circle_radius * 2);
	QRect rect_mb(start_point.x() + shape_width / 2 - circle_radius, start_point.y() + shape_height - circle_radius, circle_radius * 2, circle_radius * 2);
	if (rect_mt.contains(point))
	{
		nodes_index = 1;
		return;
	}
	if (rect_lm.contains(point))
	{
		nodes_index = 2;
		return;
	}
	if (rect_rm.contains(point))
	{
		nodes_index = 3;
		return;
	}
	if (rect_mb.contains(point))
	{
		nodes_index = 4;
		return;
	}
}

void CircleShape::getNodePoint(QPoint& point, int index) const
{
	if (index == 1)
	{
		point.setX(start_point.x() + shape_width / 2);
		point.setY(start_point.y());
	}
	if (index == 2)
	{
		point.setX(start_point.x());
		point.setY(start_point.y() + shape_height / 2);
	}
	if (index == 3)
	{
		point.setX(start_point.x() + shape_width);
		point.setY(start_point.y() + shape_height / 2);
	}
	if (index == 4)
	{
		point.setX(start_point.x() + shape_width / 2);
		point.setY(start_point.y() + shape_height);
	}
}

ShapeType CircleShape::getType() const
{
	return ShapeType::Circle;
}

double CircleShape::getRate()
{
	return 1.0;
}
